<?php
/**
 * 消费订单提交接口
 */
namespace Api\Controller;

use Think\Controller;

class WxnotifyController extends Controller {
	public $oauth_user = array();

	public function _initialize() {
        $this->m_card_push = M("CardPush");
        $this->m_card = M("Card");
        // $_SESSION['open_id'] = 'o8j7Gt-iufItn2bk4guJR9j_QimE';
        // $_SESSION['user_token'] = 'wx';
        // $_SESSION['token_time'] = time();
        
        // dump($this->oauth_user);
	}

    public $return_code = array(
        '1000'=>'成功',
        '1001'=>'系统错误',
        '1002'=>'操作员不存在',
        '1003'=>'操作员密码错误',
        '1004'=>'操作员被锁定',
        '1005'=>'硬件号码已存在',
        '1006'=>'硬件号码不存在',
    );

    /*充值推送*/
    public function push_recharge() {
        $detail_id = I("request.detail_id");

        // $detail_id = '2945';

        $recharge_data = M('Recharge')->where(array("id"=>$detail_id))->find();
        $recharge_data['total_add_point'] = $recharge_data['general_point'] + $recharge_data['self_point'];

        $card_data = M('Card')->where(array('id'=>$recharge_data['rid']))->find();

        $card_auth = M("CardPush")->where(array("card_number"=>$card_data['number']))->select();
        $open_ids = array();
        if(!empty($card_auth)) foreach ($card_auth as $_key => $_row) {
            $open_ids[] = $_row['open_id'];
        }

        if ($recharge_data['sale_id']) {
            $sale = M("Sale")->where(array("id"=>$recharge_data['sale_id']))->find();
            $recharge_data['opt_settle'] = $sale['sale_name'];
        }else{
            $logistics = M("Logistics")->where(array("id"=>$recharge_data['lid']))->find();
            $recharge_data['opt_settle'] = $logistics['logistics_name'];
        }

        $opt_user = M('users')->where(array('id'=>$recharge_data['uid']))->find();

        $data = array(
            'first' => array(
                // 'name' => '流水号',
                'value' => '卡片充值流水号：'.$recharge_data['number'],
                'color' => '#173177'
            ),
            'keyword1' => array(
                // 'name' => '充值卡号',
                'value' => $card_data['number'],
                'color' => '#173177'
            ),
            'keyword2' => array(
                // 'name' => '充值机构',
                'value' => $recharge_data['opt_settle'].'',
                'color' => '#173177'
            ),
            'keyword3' => array(
                // 'name' => '充值金额',
                'value' => $recharge_data['price'],
                'color' => '#173177'
            ),
            'keyword4' => array(
                // 'name' => '卡内余额',
                'value' => $card_data['balance'].'',
                'color' => '#173177'
            ),
            'keyword5' => array(
                // 'name' => '充值时间',
                'value' => date("Y-m-d H:i:s",$recharge_data['create_time']),
                'color' => '#173177'
            ),
            'remark' => array(
                // 'name' => '卡内积分',
                'value' => '卡内积分：'.$card_data['point'].'',
                'color' => '#173177'
            ),
        );
        // pfVU6U1QxVeMSn0DitO5t3pUR409OryBRjnN-WQSpgc
        // HFV6RjawLMMVAbS29P2dvXw7_qlVqTBV41Or0BmWOWQ - 测试
        // BPUGtwv778CktC-aHMZIvQqASmMbMhwwAFnHrMD-vjY - 正式
        $success_count = $this->push_by_card($open_ids,'hDIjJ-rwvEj596xWWytSw9Xft_zYoL_XAdh9qd82oQA',$data);
        exit(json_encode(array('code'=>$success_count,'msg'=>$success_count?'推送成功,'.$success_count.'条':'推送失败','open_ids'=>$open_ids)));
    }

    /*充值推送*/
    public function push_sale() {
        $detail_id = I("request.detail_id");

        // $detail_id = 7170;

        $sale_data = M("CardSale")->where(array("id"=>$detail_id))->find();

        $card_auth = M("CardPush")->where(array("card_number"=>$sale_data['card_number']))->select();
        $open_ids = array();
        if(!empty($card_auth)) foreach ($card_auth as $_key => $_row) {
            $open_ids[] = $_row['open_id'];
        }

        $sale_data['total_add_point'] = $sale_data['general_point'] + $sale_data['self_point'];
        $sale = M("Sale")->where(array("id"=>$sale_data['sale_id']))->find();
        $sale_user = M('users')->where(array('id'=>$sale_data['uid']))->find();

        $data = array(
            'first' => array(
                'value' => $sale['sale_name'].'-消费：'.$sale_data['number'],
                'color' => '#173177'
            ),
            'keyword1' => array(
                // 'name' => '消费卡号',
                'value' => $sale_data['card_number'],
                'color' => '#173177'
            ),
            'keyword2' => array(
                'name' => '实付金额',
                'value' => $sale_data['price'].'',
                'color' => '#173177'
            ),
            'keyword3' => array(
                'name' => '卡内余额',
                'value' => $sale_data['plus_price'],
                'color' => '#173177'
            ),
            'keyword4' => array(
                'name' => '卡内积分',
                'value' => $sale_data['plus_point'],
                'color' => '#173177'
            ),
            'keyword5' => array(
                // 'name' => '消费时间',
                'value' => date("Y-m-d H:i:s",$sale_data['create_time']),
                'color' => '#173177'
            ),
            'remark' => array(
                'value' => $sale_data['goods_name'].$sale_data['goods_unit_price'].'元/'.$sale_data['goods_unit'].' x'.$sale_data['goods_num'].$sale_data['goods_unit'],
                'color' => '#173177'
            ),
            
        );
        
        // eof9829qDJZK0qhx8RSAHaNV1kp-Hrgd-wQbEO3CWEc
        // jok85n8h3gHmcSWi9iOW8s3XagyNxEZNlixf4HlDz64 - 测试
        // hDIjJ-rwvEj596xWWytSw9Xft_zYoL_XAdh9qd82oQA - 正式
        $success_count = $this->push_by_card($open_ids,'BPUGtwv778CktC-aHMZIvQqASmMbMhwwAFnHrMD-vjY',$data);
        exit(json_encode(array('code'=>$success_count,'msg'=>$success_count?'推送成功,'.$success_count.'条':'推送失败','open_ids'=>$open_ids)));
    }

    /*充值推送*/
    public function push_examine() {
        $examine_id = I("request.examine_id");

        // $examine_id = 7170;

        $sale_data = M("CardSale")->where(array("id"=>$examine_id))->find();

        $card_auth = M("CardPush")->where(array("card_number"=>$sale_data['card_number']))->select();
        $open_ids = array();
        if(!empty($card_auth)) foreach ($card_auth as $_key => $_row) {
            $open_ids[] = $_row['open_id'];
        }

        $sale_data['total_add_point'] = $sale_data['general_point'] + $sale_data['self_point'];
        $sale = M("Sale")->where(array("id"=>$sale_data['sale_id']))->find();
        $sale_user = M('users')->where(array('id'=>$sale_data['uid']))->find();
        
        $data = array(
            'first' => array(
                'value' => '您有一条充值审核单子未审核',
                'color' => '#173177'
            ),
            'keyword1' => array(
                // 'name' => '消费卡号',
                'value' => $sale_data['card_number'],
                'color' => '#173177'
            ),
            'keyword1' => array(
                // 'name' => '消费卡号',
                'value' => $sale_data['card_number'],
                'color' => '#173177'
            ),
            'remark' => array(
                'value' => $sale_data['goods_name'].$sale_data['goods_unit_price'].'元/'.$sale_data['goods_unit'].' x'.$sale_data['goods_num'].$sale_data['goods_unit'],
                'color' => '#173177'
            ),
            
        );
        // eof9829qDJZK0qhx8RSAHaNV1kp-Hrgd-wQbEO3CWEc
        // jok85n8h3gHmcSWi9iOW8s3XagyNxEZNlixf4HlDz64 - 测试
        $success_count = $this->push_by_card($open_ids,'jok85n8h3gHmcSWi9iOW8s3XagyNxEZNlixf4HlDz64',$data);
        exit(json_encode(array('code'=>$success_count,'msg'=>$success_count?'推送成功':'推送失败')));
    }

    public function push_by_card($open_ids,$template_id,$push_data){
        $wechat = new \Common\Lib\wechat();
        $success_count = 0;
        if(!empty($open_ids)) foreach ($open_ids as $key => $open_id) {
            // dump($open_id);
            // dump($template_id);
            $result = $wechat->sendTemplateMessage($open_id, $template_id, $push_data);
            if ($result['errcode'] == 0) {
                $success_count++;
            }
        }
        return $success_count;
    }

    public function bind_card_post(){
        
        $cards = trim($_REQUEST['cards']);;
        
        if (empty($cards)) {
            exit(json_encode(array('code'=>0,'msg'=>'请输入卡号')));
        }

        $cards = explode("\n", $cards);
        $open_id = $_SESSION['open_id'];


        /*验证卡号*/
        $exist_cards = $this->m_card->where(array('number'=>array('in',$cards)))->select();
        if (empty($exist_cards)) {
            exit(json_encode(array('code'=>0,'msg'=>'卡号输入错误')));
        }

        $insert_list = array();
        // dump($exist_cards);
        foreach ($exist_cards as $_key => $_row) {
            $insert_list[] = array('open_id'=>$open_id,'card_number'=>$_row['number']);
        }
        $result = $this->m_card_push->addAll($insert_list);

        if ($result) {
            exit(json_encode(array('code'=>$result,'msg'=>'')));
        } else {
            exit(json_encode(array('code'=>0,'msg'=>'系统错误')));
        }
    }

    /*绑定微信卡号*/
    public function bind_card() {
        $wechat = new \Common\Lib\wechat();
        $this->oauth_user = $wechat->wxauth();
        $this->assign('auth',$this->oauth_user);
        $this->display();
    }

    /*绑定的卡号列表*/
    public function bind_list() {
        try{
            $open_id = $_SESSION['open_id'];
            $card_data = $this->m_card_push->where(array("open_id"=>$open_id))->select();
            exit(json_encode(array('code'=>1,'msg'=>'','data'=>$card_data)));
        }catch (\Exception $e){
            exit(json_encode(array('code'=>0,'msg'=>'系统错误')));
        }
    }

    /*绑定的卡号列表*/
    public function unbind_card() {
        try{
            $open_id = $_SESSION['open_id'];
            $card_number = I("post.card_number");
            $flag = $this->m_card_push->where(array("open_id"=>$open_id,'card_number'=>$card_number))->delete();

            exit(json_encode(array('code'=>$flag,'msg'=>$flag?'删除成功':'删除失败')));

        }catch (\Exception $e){
            exit(json_encode(array('code'=>1001,'msg'=>'解绑失败','softid'=>'')));
        }
    }

}

